define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index' + location.search,
                    add_url: 'order/order/add',
                    edit_url: 'order/order/edit',
                    del_url: 'order/order/del',
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });
            var table = $("#table");
            var refundStatus ={
                "1":"退款中",
                "2":"退款成功",
                "3":"退款失败",
            };

            var statusList2 = {
                "0" : __('Status 0'), '1' : __('Status 1'), '2' : __('Status 2'), '3' : __('Status 3'), '4' : __('Status 4'),
                '5' : __('Status 5'), '6' : __('Status 6'), '7' : __('Status 7'),'8' : __('Status 8'), '9' : __('Status 9'),
                '10' : __('Status 10'),'11' : __('Status 11'),'12' : __('Status 12'),'13' : __('Status 13'),
            };

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'shop.shop_name', title: __('Shop.shop_name'), operate: 'LIKE',
                            visible:(shop_id > 0) ? false : true,
                        },
                        {field: 'order_number', title: __('Order_number'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('Order_user'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: (shop_id > 0 ? statusList : statusList2),
                            formatter:function (value,row){
                                if (row.refund_status > 0){
                                    return statusList[value]+'<span style="color: red">(' + refundStatus[row.refund_status] + ')</span>';
                                }else{
                                    return statusList[value];
                                }
                            }
                        },
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE'},
                        {field: 'num', title: __('Num')},
                        {field: 'actual_price', title: __('Actual_price'), operate:'BETWEEN'},
                        {field: 'harvest_user', title: __('Harvest_user'), operate: 'LIKE'},
                        {field: 'harvest_phone', title: __('Harvest_phone'), operate: 'LIKE'},
                        {field: 'order_time', title: __('Order_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'payment_time', title: __('Payment_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '详情',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'order/order/detail'
                                },
                                {
                                    name: 'up_status',
                                    text: '取消接单',
                                    icon: 'fa fa-search-plus',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'order/order/up_status?type=1',
                                    confirm:'是否取消接单?',
                                    success: function () {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    visible:function (row){
                                        if(row.status == '2'){
                                            return true;
                                        }
                                        return false;
                                    },
                                },
                                {
                                    name: 'up_status',
                                    text: '确认接单',
                                    icon: 'fa fa-search-plus',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'order/order/up_status?type=2',
                                    confirm:'是否确认接单?',
                                    success: function () {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    visible:function (row){
                                        if(row.status == '2' && shop_id > 0){
                                            return true;
                                        }
                                        return false;
                                    },
                                },
                                {
                                    name: 'up_status',
                                    text: '确认制作完成',
                                    icon: 'fa fa-search-plus',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'order/order/up_status?type=3',
                                    confirm:'是否确认制作完成?',
                                    success: function () {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    visible:function (row){
                                        if(row.status == '3' && shop_id > 0){
                                            return true;
                                        }
                                        return false;
                                    },
                                },
                                {
                                    name: 'up_status',
                                    text: '打印小票',
                                    icon: 'fa fa-search-plus',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'order/order/print',
                                    confirm:'是否打印小票?',
                                    success: function () {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    visible:function (){
                                        if(shop_id > 0){
                                            return true;
                                        }
                                        return false;
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        withdraw: function () {
            Form.api.bindevent($("form"), function(data,ret){
                window.parent.location.reload();
            }, function(data, ret){
                //失败的回调,如果返回false,将不会出现提示消息
            }, function(success, error){
                Controller.api.bindevent();
            });
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
